package com.keke.beauty.auth.activityInvolvement.controller;

import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.keke.beauty.auth.activity.entity.TabActivity;
import com.keke.beauty.auth.activity.service.TabActivityService;
import com.keke.beauty.auth.activityInvolvement.service.TabActivityInvolvementService;
import com.keke.beauty.sys.user.entity.SysUser;
import com.keke.beauty.utils.pojo.Result;
import com.keke.beauty.utils.pojo.TableInfo;
import com.keke.beauty.utils.tools.ResultUtil;

import lombok.extern.slf4j.Slf4j;
/**
 * 活动参与控制层
 * @author 
 * @Data 2019年02月27日 11:06:36
 */
@Slf4j
@RestController
@RequestMapping("activityInvolvement")
public class TabActivityInvolvementController {
    
    @Autowired
    private TabActivityInvolvementService service;

    @Autowired
    private TabActivityService activityService;
    
    /**
     * 活动参与列表
     * @author 
     * @Data 2019年02月27日 11:06:36
     * @Date 2018年1月15日 下午2:29:08
     * @param tableInfo
     * @return
     * @throws Exception 
     */
    @GetMapping
    public Result getList(TableInfo<Map<String, Object>> tableInfo,String isSignUp){
        SysUser user = (SysUser) SecurityUtils.getSubject().getPrincipal();
        TabActivity activity = activityService.findByUserId(user.getId());
        Integer activityId = null;
        if(null != activity){
            activityId = activity.getId();
        }
        PageHelper.startPage(tableInfo.getPageNum(), tableInfo.getPageSize());
        PageHelper.orderBy("tai.create_time desc");
        PageInfo<Map<String, Object>> page = new PageInfo<Map<String, Object>>(service.getList(tableInfo.getSearch(), activityId,isSignUp));
        return ResultUtil.successTable(page);
    }
    
    @GetMapping("downloadExcel")
    public void downloadExcel(HttpServletResponse response, String search, String isSignUp) throws UnsupportedEncodingException{
        response.reset();
        response.setContentType("application/octet-stream; charset=utf-8");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        response.setHeader("Content-Disposition", "attachment;filename="+ URLEncoder.encode("AddressList_"+ sdf.format(new Date())+ ".xlsx", "utf-8"));
        SysUser user = (SysUser) SecurityUtils.getSubject().getPrincipal();
        TabActivity activity = activityService.findByUserId(user.getId());
        if(activity==null){
            activity = new TabActivity();
        }
        try {
            OutputStream out = response.getOutputStream();
            service.downloadExcel(out, search, activity.getId(),isSignUp);
            out.flush();
        } catch (IOException e) {
            e.printStackTrace();
            log.error("excel导出失败");
        }
    }
    
}